﻿package com.projects.xenia.add_activities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.projects.xenia.R;

import com.projects.xenia.documents_classes.Documents_Order;
import com.projects.xenia.documents_classes.Documents_Order.DocLine;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.prompts_activities.Activity_NumericPrompt;

import com.projects.xenia.ref_classes.GoodRefObject;
import com.projects.xenia.ref_classes.GoodsRefItem;
import com.projects.xenia.ref_classes.OffersConditionsAssortmensRefItem;
import com.projects.xenia.ref_classes.OffersConditionsRefItem;
import com.projects.xenia.ref_classes.OffersRefItem;


public class Activity_OffersAssortments extends Activity {

	
	private ListView AssortmentsListView; 
	private AssortmentsCustomListAdapter AssortmentsListViewAdapter;
	
	private Context Ctx;

	private Documents_Order DocumentClass;
	
	private String CustomersPriceCategory;
	
	private ImageView offers_form_BackWard;	
	
	
	private OffersConditionsRefItem Offers_Condition;
	
	private TextView Offers_ConditionTextView;
	private TextView Offers_ConditionValueTextView;
	
	
	private void finishActivity () {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(Ctx);
		builder.setMessage("Перенести изменения ?");
		builder.setPositiveButton("Да", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {	
				
				Intent DataIntent = new Intent ();
				setResult(RESULT_OK, DataIntent);
				
				Bundle DocExtra = new Bundle ();
				for ( OffersConditionsAssortmensRefItem it : Offers_Condition.offers_assortmets_items) {
					DocExtra.putDouble(it.goods.id, it.offers_condition_count_value);
				}		
						
				DataIntent.putExtras(DocExtra);
				
				finish();
				
			}
		});
		
		builder.setNegativeButton("Нет", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {			
				finish ();
			}
		});
		
		builder.create().show();
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	
		setContentView(R.layout.offers_form);		
		
		offers_form_BackWard = (ImageView)findViewById(R.id.offers_form_BackWard);
		offers_form_BackWard.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finishActivity ();
			}
		});
		
		DocumentClass = (Documents_Order)MainGlobalModule.DocumentClass;
		CustomersPriceCategory = MainGlobalModule.GetCustomerPriceCategoy(DocumentClass.Head.customers_id);
		
		Ctx = this;				
		
		Offers_Condition = new OffersConditionsRefItem();
		
		Bundle DocExtras;
		DocExtras = getIntent().getExtras();
		
		Offers_ConditionTextView = (TextView)findViewById(R.id.offers_form_descr);		
		Offers_ConditionTextView.setText("Список товаров условия");
		
		Offers_ConditionValueTextView = (TextView)findViewById(R.id.offers_form_descrvalue);	
		
		Offers_Condition.id = DocExtras.getString("id");DocExtras.remove("id");		
		Offers_Condition.offers_id = DocExtras.getString("offers_id");DocExtras.remove("offers_id");
		Offers_Condition.offers_condition_numb = DocExtras.getInt("offers_condition_numb");DocExtras.remove("offers_condition_numb");		
		Offers_Condition.offers_condition_sku_count = DocExtras.getInt("offers_condition_sku_count");DocExtras.remove("offers_condition_sku_count");
		Offers_Condition.offers_condition_value = DocExtras.getDouble("offers_condition_value");DocExtras.remove("offers_condition_value");		
		Offers_Condition.offers_condition_sku_count_value = DocExtras.getInt("offers_condition_sku_count_value");DocExtras.remove("offers_condition_sku_count_value");
		Offers_Condition.offers_condition_count_value = DocExtras.getDouble("offers_condition_count_value");DocExtras.remove("offers_condition_count_value");
		Offers_Condition.offers_condition_sum_value = DocExtras.getDouble("offers_condition_sum_value");DocExtras.remove("offers_condition_sum_value");	
		
		
		Offers_Condition.offers_assortmets_items = new ArrayList<OffersConditionsAssortmensRefItem>();
		
		Set<String> keys = DocExtras.keySet();
		
		
	    for (String key : keys) { 	    	
	    	OffersConditionsAssortmensRefItem it = new OffersConditionsAssortmensRefItem();	    	
	    	it.offers_condition_id = "";
	    	it.goods = MainGlobalModule.GetGoodsRefItemByGoodsId(key, CustomersPriceCategory , DocumentClass.Head.firms_id, DocumentClass.Head.acctype, DocumentClass.Head.departments_id);
	    	it.offers_condition_sku_count = 0;
	    	it.offers_condition_value = 0;	    	
	    	it.offers_condition_sku_count_value = 0;
	    	it.offers_condition_count_value = (Double) DocExtras.get(key);
	    	it.offers_condition_sum_value = it.goods.price*it.offers_condition_count_value;
	    	Offers_Condition.offers_assortmets_items.add(it);
	    }
	    
	    
		AssortmentsListView = (ListView)findViewById(R.id.offers_form_ListView);
		AssortmentsListViewAdapter = new AssortmentsCustomListAdapter (this);
		AssortmentsListView.setAdapter(AssortmentsListViewAdapter);		
		AssortmentsListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {				
				OffersConditionsAssortmensRefItem it = AssortmentsListViewAdapter.getItem(arg2);				
				Intent NumericPrompt = new Intent ();
				NumericPrompt.setClass(Ctx, Activity_NumericPrompt.class);
				NumericPrompt.putExtra("Numeric", it.offers_condition_count_value);				
				startActivityForResult(NumericPrompt, arg2);
			}
		});
		
	}	
	
	private void RecalculateOffer () {
	
		int sku_total = 0;
		double count_total = 0;
		double sum_total = 0;
		
		for (OffersConditionsAssortmensRefItem itx : Offers_Condition.offers_assortmets_items) {				
			if (itx.offers_condition_count_value>0){
				sku_total += 1;
				count_total += itx.offers_condition_count_value;
				sum_total += itx.offers_condition_sum_value;		
			}	
				Offers_Condition.offers_condition_sku_count_value = sku_total;
				Offers_Condition.offers_condition_count_value = count_total;
				Offers_Condition.offers_condition_sum_value = sum_total;
		}
		
		Offers_ConditionValueTextView.setText(String.format("SKU (%d/%d)   Кол-во (%.2f/%.2f)    Сумма (%.2f)", Offers_Condition.offers_condition_sku_count,Offers_Condition.offers_condition_sku_count_value, Offers_Condition.offers_condition_value,Offers_Condition.offers_condition_count_value,Offers_Condition.offers_condition_sum_value));
		
		AssortmentsListViewAdapter.notifyDataSetChanged();
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == RESULT_OK) {
			
			OffersConditionsAssortmensRefItem it = AssortmentsListViewAdapter.getItem(requestCode);	
			it.offers_condition_count_value = data.getDoubleExtra("NumericValue", 0);
			it.offers_condition_sum_value = it.offers_condition_count_value*it.goods.price;
			AssortmentsListViewAdapter.notifyDataSetChanged();
			RecalculateOffer ();
			
		}		
		
	}
	
	private class AssortmentsCustomListAdapter extends BaseAdapter  {

		private LayoutInflater mInflater;
		public Context context;		
		  
		public AssortmentsCustomListAdapter(Context context) { 
			
			mInflater = LayoutInflater.from(context);
			this.context = context;
			
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		    
			
			if (convertView == null) {			
				convertView = mInflater.inflate(R.layout.offersconditions_form_sublist, null);
			}
			
		
			convertView.setBackgroundColor(Color.rgb(0, 0, 0));
				
			
			OffersConditionsAssortmensRefItem it  = getItem(position);
			
			TextView offerscondass_descr = (TextView) convertView.findViewById(R.id.offerscondass_descr);
			offerscondass_descr.setText(it.goods.descr);
			offerscondass_descr.setTextColor(Color.WHITE);
			
			TextView offerscondass_price = (TextView) convertView.findViewById(R.id.offerscondass_price);
			offerscondass_price.setText(String.format("ц.%.2f", it.goods.price));
			
			TextView offerscondass_rest = (TextView) convertView.findViewById(R.id.offerscondass_rest);
			offerscondass_rest.setText(String.format("о.%.2f %s", it.goods.rest,it.goods.measure_base_descr));					
			if (it.goods.rest<=0) {
				offerscondass_descr.setTextColor(Color.DKGRAY);	
			}
			
			TextView offerscondass_value = (TextView) convertView.findViewById(R.id.offerscondass_value);
			offerscondass_value.setText(String.format("%.2f",it.offers_condition_value));
			if (it.offers_condition_value==0){
				offerscondass_value.setText("");
			}
			
			TextView offerscondass_value_count = (TextView) convertView.findViewById(R.id.offerscondass_value_count);
			offerscondass_value_count.setText(String.format("%.2f",it.offers_condition_count_value));

			TextView offerscondass_value_sum = (TextView) convertView.findViewById(R.id.offerscondass_value_sum);
			offerscondass_value_sum.setText(String.format("%.2f",it.offers_condition_sum_value));

			TextView offerscondass_ordercount = (TextView) convertView.findViewById(R.id.offerscondass_ordercount);			
			TextView offerscondass_ordersum = (TextView) convertView.findViewById(R.id.offerscondass_ordersum);
			
			DocLine docit = DocumentClass.DocLineCollection.get(it.goods.id);
			if (!(docit==null)) {				
				double ordervalue = docit.ordervalue*docit.factor; 
				offerscondass_ordercount.setText(String.format("%.2f %s",ordervalue,it.goods.measure_base_descr));
				offerscondass_ordersum.setText(String.format("%.2f",docit.order_sum));				
				if (ordervalue >= it.offers_condition_value) {
					convertView.setBackgroundColor(Color.rgb(130, 130, 130));					
				}							
			}	
			else
			{
				offerscondass_ordercount.setText("");
				offerscondass_ordersum.setText("");
			}
			
			if ((it.offers_condition_count_value > 0) & (it.offers_condition_count_value >= it.offers_condition_value)) {
				convertView.setBackgroundColor(Color.rgb(118, 39, 95));
			}
			
			if (it.goods.isfolder==1) {
				convertView.setBackgroundColor(Color.CYAN);	
			}
			
			TextView offers_condition_ok_value = (TextView) convertView.findViewById(R.id.offers_condition_ok_value);
			offers_condition_ok_value.setVisibility(View.INVISIBLE);
			
			return convertView;
			
		}
		
		@Override
		public int getCount() {			
			return Offers_Condition.offers_assortmets_items.size();
		}

		@Override
		public OffersConditionsAssortmensRefItem getItem(int arg0) {
			return Offers_Condition.offers_assortmets_items.get(arg0);			
		}

		@Override
		public long getItemId(int arg0) {			
			return Offers_Condition.offers_assortmets_items.get(arg0).hashCode();
		}	 
		
	}
	
	
}
